﻿package com.game.engine.nape
{
    import com.game.engine.nape.utils.BodyUserData;
    import com.game.engine.nape.utils.CBTypes;
    import com.game.engine.phys.Collision2D;
    import com.game.engine.phys.Vector2D;
    
    import nape.callbacks.CbEvent;
    import nape.callbacks.CbType;
    import nape.callbacks.InteractionCallback;
    import nape.callbacks.InteractionListener;
    import nape.callbacks.InteractionType;
    import nape.callbacks.PreCallback;
    import nape.callbacks.PreFlag;
    import nape.callbacks.PreListener;
    import nape.dynamics.CollisionArbiter;
    import nape.space.Space;
    
    public class InteractionListeners
    {

        public function InteractionListeners(space:Space)
        {
            super();
            var lis:InteractionListener;
            var pre:PreListener;
            lis = new InteractionListener(CbEvent.BEGIN, InteractionType.SENSOR, CbType.ANY_BODY, CbType.ANY_BODY, handSensorEnter);
            space.listeners.add(lis);
            lis = new InteractionListener(CbEvent.ONGOING, InteractionType.SENSOR, CbType.ANY_BODY, CbType.ANY_BODY, handSensorOnGoing);
            space.listeners.add(lis);
            lis = new InteractionListener(CbEvent.END, InteractionType.SENSOR, CbType.ANY_BODY, CbType.ANY_BODY, handSensorEnd);
            space.listeners.add(lis);
            lis = new InteractionListener(CbEvent.BEGIN, InteractionType.COLLISION, CbType.ANY_BODY, CbType.ANY_BODY, handCollisionBegin);
            space.listeners.add(lis);
            lis = new InteractionListener(CbEvent.END, InteractionType.COLLISION, CbType.ANY_BODY, CbType.ANY_BODY, handCollisionEnd);
            space.listeners.add(lis);
            lis = new InteractionListener(CbEvent.ONGOING, InteractionType.COLLISION, CbType.ANY_BODY, CbType.ANY_BODY, handCollisionOnGoing);
            space.listeners.add(lis);
            lis = new InteractionListener(CbEvent.BEGIN, InteractionType.FLUID, CbType.ANY_BODY, CbType.ANY_BODY, handFluidBegin);
            space.listeners.add(lis);
            lis = new InteractionListener(CbEvent.END, InteractionType.FLUID, CbType.ANY_BODY, CbType.ANY_BODY, handFluidEnd);
            space.listeners.add(lis);
            pre = new PreListener(InteractionType.COLLISION, CBTypes.manCbType, CBTypes.floorCbType, handPreListener);
            space.listeners.add(pre);
//            pre = new PreListener(InteractionType.COLLISION, CBTypes.manCbType, CBTypes.groundType, handPreListener3);
//            space.listeners.add(pre);
        }

        private function handCollisionOnGoing(callBack:InteractionCallback):void
        {
            var _local_6:* = null;
            var _local_7:* = null;
            var _local_4:* = null;
            var _local_5:* = null;
            var _local_2:BodyUserData = callBack.int1.castBody.userData.data;
            var _local_3:BodyUserData = callBack.int2.castBody.userData.data;
            if (callBack.arbiters.length > 0)
            {
                _local_7 = (callBack.arbiters.at(0) as CollisionArbiter);
                _local_6 = new Vector2D(_local_7.normal.x, _local_7.normal.y);
            }
            if (_local_2 && _local_3)
            {
                _local_4 = new Collision2D(_local_2.gameObject, _local_2.body, _local_3.gameObject, _local_3.body, _local_6);
                _local_4.interactionCallback = callBack;
                if (_local_2 && _local_2.gameObject)
                {
                    _local_2.gameObject.onCollisionOnGoing(_local_4);
                }
                if (_local_6)
                {
                    _local_6 = _local_6.multiply(-1);
                }
                _local_5 = new Collision2D(_local_3.gameObject, _local_3.body, _local_2.gameObject, _local_2.body, _local_6);
                _local_5.interactionCallback = callBack;
                if (_local_3 && _local_3.gameObject)
                {
                    _local_3.gameObject.onCollisionOnGoing(_local_5);
                }
            }
        }

        private function handCollisionEnd(callBack:InteractionCallback):void
        {
            var _local_6:* = null;
            var _local_7:* = null;
            var _local_4:* = null;
            var _local_5:* = null;
            var _local_2:BodyUserData = callBack.int1.castBody.userData.data;
            var _local_3:BodyUserData = callBack.int2.castBody.userData.data;
            if (callBack.arbiters.length > 0)
            {
                _local_7 = (callBack.arbiters.at(0) as CollisionArbiter);
                _local_6 = new Vector2D(_local_7.normal.x, _local_7.normal.y);
            }
            if (_local_2 && _local_3)
            {
                _local_4 = new Collision2D(_local_2.gameObject, _local_2.body, _local_3.gameObject, _local_3.body, _local_6);
                if (_local_2 && _local_2.gameObject)
                {
                    _local_2.gameObject.onCollisionExit(_local_4);
                }
                if (_local_6)
                {
                    _local_6 = _local_6.multiply(-1);
                }
                _local_5 = new Collision2D(_local_3.gameObject, _local_3.body, _local_2.gameObject, _local_2.body, _local_6);
                if (_local_3 && _local_3.gameObject)
                {
                    _local_3.gameObject.onCollisionExit(_local_5);
                }
            }
        }

        private function handCollisionBegin(callBack:InteractionCallback):void
        {
            var _local_6:* = null;
            var _local_7:* = null;
            var _local_4:* = null;
            var _local_5:* = null;
            var _local_2:BodyUserData = callBack.int1.castBody.userData.data;
            var _local_3:BodyUserData = callBack.int2.castBody.userData.data;
            if (callBack.arbiters.length > 0)
            {
                _local_7 = (callBack.arbiters.at(0) as CollisionArbiter);
                _local_6 = new Vector2D(_local_7.normal.x, _local_7.normal.y);
            }
            if (_local_2 && _local_3)
            {
                if (_local_2.body.once == 0 || _local_3.body.once == 0)
                {
                    return;
                }
                _local_4 = new Collision2D(_local_2.gameObject, _local_2.body, _local_3.gameObject, _local_3.body, _local_6);
                if (_local_2 && _local_2.gameObject)
                {
                    _local_2.gameObject.onCollisionEnter(_local_4);
                }
                if (_local_6)
                {
                    _local_6 = _local_6.multiply(-1);
                }
                _local_5 = new Collision2D(_local_3.gameObject, _local_3.body, _local_2 ? _local_2.gameObject : null, _local_2 ? _local_2.body : null, _local_6);
                if (_local_3 && _local_3.gameObject)
                {
                    _local_3.gameObject.onCollisionEnter(_local_5);
                }
                _local_2.body.once--;
                _local_3.body.once--;
            }
        }

        private function handFluidBegin(callBack:InteractionCallback):void
        {
            var _local_4:* = null;
            var _local_5:* = null;
            var _local_2:BodyUserData = callBack.int1.castBody.userData.data;
            var _local_3:BodyUserData = callBack.int2.castBody.userData.data;
            if (_local_2 && _local_3)
            {
                _local_4 = new Collision2D(_local_2.gameObject, _local_2.body, _local_3.gameObject, _local_3.body, null);
                if (_local_2 && _local_2.gameObject)
                {
                    _local_2.gameObject.onFluidEnter(_local_4);
                }
                _local_5 = new Collision2D(_local_3.gameObject, _local_3.body, _local_2 ? _local_2.gameObject : null, _local_2 ? _local_2.body : null, null);
                if (_local_3 && _local_3.gameObject)
                {
                    _local_3.gameObject.onFluidEnter(_local_5);
                }
            }
        }

        private function handFluidEnd(callBack:InteractionCallback):void
        {
            var _local_4:* = null;
            var _local_5:* = null;
            var _local_2:BodyUserData = callBack.int1.castBody.userData.data;
            var _local_3:BodyUserData = callBack.int2.castBody.userData.data;
            if (_local_2 && _local_3)
            {
                _local_4 = new Collision2D(_local_2.gameObject, _local_2.body, _local_3.gameObject, _local_3.body, null);
                if (_local_2 && _local_2.gameObject)
                {
                    _local_2.gameObject.onFluidExit(_local_4);
                }
                _local_5 = new Collision2D(_local_3.gameObject, _local_3.body, _local_2 ? _local_2.gameObject : null, _local_2 ? _local_2.body : null, null);
                if (_local_3 && _local_3.gameObject)
                {
                    _local_3.gameObject.onFluidExit(_local_5);
                }
            }
        }

        private function handSensorEnter(callBack:InteractionCallback):void
        {
            var _local_4:* = null;
            var _local_5:* = null;
            var _local_2:BodyUserData = callBack.int1.castBody.userData.data;
            var _local_3:BodyUserData = callBack.int2.castBody.userData.data;
            if (_local_2 && _local_3)
            {
                _local_4 = new Collision2D(_local_2.gameObject, _local_2.body, _local_3.gameObject, _local_3.body, null);
                if (_local_2 && _local_2.gameObject)
                {
                    _local_2.gameObject.onSensorEnter(_local_4);
                }
                _local_5 = new Collision2D(_local_3.gameObject, _local_3.body, _local_2 ? _local_2.gameObject : null, _local_2 ? _local_2.body : null, null);
                if (_local_3 && _local_3.gameObject)
                {
                    _local_3.gameObject.onSensorEnter(_local_5);
                }
            }
        }

        private function handSensorOnGoing(callBack:InteractionCallback):void
        {
            var _local_4:* = null;
            var _local_5:* = null;
            var _local_2:BodyUserData = callBack.int1.castBody.userData.data;
            var _local_3:BodyUserData = callBack.int2.castBody.userData.data;
            if (_local_2 && _local_3)
            {
                _local_4 = new Collision2D(_local_2.gameObject, _local_2.body, _local_3.gameObject, _local_3.body, null);
                if (_local_2 && _local_2.gameObject)
                {
                    _local_2.gameObject.onSensorOnGoing(_local_4);
                }
                _local_5 = new Collision2D(_local_3.gameObject, _local_3.body, _local_2 ? _local_2.gameObject : null, _local_2 ? _local_2.body : null, null);
                if (_local_3 && _local_3.gameObject)
                {
                    _local_3.gameObject.onSensorOnGoing(_local_5);
                }
            }
        }

        private function handSensorEnd(callBack:InteractionCallback):void
        {
            var _local_4:* = null;
            var _local_5:* = null;
            var _local_2:BodyUserData = callBack.int1.castBody.userData.data;
            var _local_3:BodyUserData = callBack.int2.castBody.userData.data;
            if (_local_2 && _local_3)
            {
                _local_4 = new Collision2D(_local_2.gameObject, _local_2.body, _local_3.gameObject, _local_3.body, null);
                if (_local_2 && _local_2.gameObject)
                {
                    _local_2.gameObject.onSensorExit(_local_4);
                }
                _local_5 = new Collision2D(_local_3.gameObject, _local_3.body, _local_2.gameObject, _local_2.body, null);
                if (_local_3 && _local_3.gameObject)
                {
                    _local_3.gameObject.onSensorExit(_local_5);
                }
            }
        }

        private function handPreListener(callBack:PreCallback):PreFlag
        {
            var _local_3:* = null;
            var _local_5:* = null;
            var data1:BodyUserData = callBack.int1.castBody.userData.data;
            var data2:BodyUserData = callBack.int2.castBody.userData.data;
            var isIgnore:Boolean = Nape.getIgnoreCollision(data1.body, data2.body);
            if (isIgnore)
            {
                return PreFlag.IGNORE;
            }
            /*if (callBack.swapped)
            {
                data1 = callBack.int2.castBody.userData.data;
                data2 = callBack.int1.castBody.userData.data;
            }
            if (data1.gameObject && data1.gameObject.data && data1.gameObject.data.objectVo && data1.gameObject.data.objectVo.properties.CollisionMids)
            {
                if (data2.gameObject && data2.gameObject.getComponent(ManEntity))
                {
                    _local_3 = data2.gameObject.getComponent(ManEntity);
                    _local_5 = data1.gameObject.data.objectVo.properties.CollisionMids.split(";");
                    if (_local_3 && _local_3.userVo)
                    {
                        if (_local_5.indexOf(_local_3.userVo.charId) < 0)
                        {
                            return PreFlag.IGNORE;
                        }
                    }
                }
            }*/
            return PreFlag.ACCEPT;
        }

        private function handPreListener3(callBack:PreCallback):PreFlag
        {
            return PreFlag.IGNORE;/*
            var _local_4:* = null;
            var _local_5:* = null;
            var _local_2:BodyUserData = callBack.int1.castBody.userData.data;
            var _local_3:BodyUserData = callBack.int2.castBody.userData.data;
            if (_local_2 && _local_3)
            {
                _local_4 = new Collision2D(_local_2.gameObject, _local_2.body, _local_3.gameObject, _local_3.body, null);
                if (_local_2 && _local_2.gameObject && _local_2.gameObject.tag == Tag.fly)
                {
                    if (_local_3 && _local_3.gameObject)
                    {
                        if (_local_2.gameObject.data.collision == _local_3.gameObject)
                        {
                            return PreFlag.ACCEPT;
                        }
                    }
                    return PreFlag.IGNORE;
                }
                _local_5 = new Collision2D(_local_3.gameObject, _local_3.body, _local_2 ? _local_2.gameObject : null, _local_2 ? _local_2.body : null, null);
                if (_local_3 && _local_3.gameObject && _local_3.gameObject.tag == Tag.fly)
                {
                    if (_local_2 && _local_2.gameObject)
                    {
                        if (_local_3.gameObject.data.collision == _local_2.gameObject)
                        {
                            return PreFlag.ACCEPT;
                        }
                    }
                    return PreFlag.IGNORE;
                }
            }
            return PreFlag.ACCEPT;*/
        }
    }
}